package com.jinduplan.planmanager.ui.activity.user

import android.content.Intent
import android.view.View
import com.cxz.kotlin.baselibs.base.BaseMvpTitleActivity
import com.cxz.kotlin.baselibs.base.setPSESee
import com.cxz.kotlin.baselibs.base.setTextChageListener
import com.cxz.kotlin.baselibs.utils.*
import com.jinduplan.planmanager.R
import com.jinduplan.planmanager.bean.LoginInfo
import com.jinduplan.planmanager.constant.Constant
import com.jinduplan.planmanager.mvp.contract.LoginRegisterContract
import com.jinduplan.planmanager.mvp.presenter.LoginRegisterPresenter
import com.jinduplan.planmanager.ui.activity.MainActivity
import kotlinx.android.synthetic.main.activity_login.*

class LoginAcitivity :
    BaseMvpTitleActivity<LoginRegisterContract.View, LoginRegisterContract.Presenter>(),
    LoginRegisterContract.View {




    override fun attachChildLayoutRes(): Int {
        return R.layout.activity_login
    }


    override fun createPresenter(): LoginRegisterContract.Presenter {
        return LoginRegisterPresenter()
    }

    override fun start() {

        findViewById<View>(R.id.app_bar_layout).visibility = View.GONE
        etAccount.setTextChageListener {
            btnLogin.isEnabled = etPsw.text.toString().length > 0 && it.length > 0
        }
        etPsw.setTextChageListener {
            btnLogin.isEnabled = etAccount.text.toString().length > 0 && it.length > 0
        }
        btnLogin.setOnClickListener {
            mPresenter?.login(etAccount.text.toString(), etPsw.text.toString())

        }
        tvGoRegister.setOnClickListener {
            startActivity(Intent(this, RegisterAcitivity::class.java))
        }
        tvForgetPsw.setOnClickListener {
            goActivity(ChangePswAcitivity::class.java)
        }
        ivSeePsw.setTag(false)
        ivSeePsw.setOnClickListener {
            etPsw.setPSESee(ivSeePsw)
        }

    }

    override fun getCodeSuccess() {

    }

    override fun getCodeFail() {

    }

    override fun loginSuccess(data: LoginInfo) {
        LogUtil.d("data->${GsonHelper.toJson(data)}")
//        LogUtil.d("login->${login}")
        UserInfoUtil.LoginSuccess(this,data.user_name,data.nick_name,data.score,data.position,data.avatar,data.labour,data.id)
        this.finish()
    }

    override fun changePswSuccess(data: LoginInfo) {

    }

    override fun registerSuccess(data: LoginInfo) {
    }

    override fun getSms(code: String) {
    }

    override fun onBackPressed() {
        super.onBackPressed()
        AppManager.getAppManager().AppExit()
    }
}